local skel = fk.CreateSkill {
  name = "emo__xunbu",
}

Fk:loadTranslationTable{
  ["emo__xunbu"] = "寻捕",
  [":emo__xunbu"] = "当牌堆的百十位数变化后，你可以从牌堆获得对应点数的牌。",
  -- 百十位：百位数与十位数，例132获得K，103获得10，16获得A，没有则不获得
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return data.extra_data and data.extra_data.emo__xunbu_num
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = data.extra_data.emo__xunbu_num
    local ids = room:getCardsFromPileByRule(".|" .. num)
    if #ids > 0 then
      room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player.seat ~= 1 then return end
    return data.extra_data and data.extra_data.emo__xunbu_orig
  end,
  on_refresh = function (self, event, target, player, data)
    local num = (#player.room.draw_pile // 10) % 100
    if num > 0 and num < 14 then
      local orig = data.extra_data.emo__xunbu_orig
      if num ~= orig then
        data.extra_data.emo__xunbu_num = num
      end
    end
  end,
})

skel:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player.seat ~= 1 then return end
    for _, move in ipairs(data) do
      if move.toArea == Card.DrawPile then return true end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DrawPile then
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local num = (#player.room.draw_pile // 10) % 100
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__xunbu_orig = num
  end,
})

return skel
